package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.VoucherInStockOrderDetail;
import com.turing.post.organization.util.VoucherInStockOrderDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherInStockOrderDetailDaoImpl")
@Remote
@Local
public class VoucherInStockOrderDetailDaoImpl implements
		VoucherInStockOrderDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据条件查询凭证入库单明细列表
	 * 
	 * @param voucherInStockOrderId
	 *            入库单ID
	 * @param page
	 *            页码
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherInStockOrderDetail> getVoucherInStockOrderDetailByVoucherInStockOrderId(
			PCPSWebInput webInput, String voucherInStockOrderId, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| PCPSUtil.isEmpty(voucherInStockOrderId))
			return null;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from VoucherInStockOrderDetail t where t.voucherInStockOrderId='"
				+ voucherInStockOrderId + "' order by t.SeqNo";
		Query query = _em.createNativeQuery(sql,
				VoucherInStockOrderDetail.class).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		return query.getResultList();
	}

	/**
	 * 根据条件查询凭证入库单明细
	 * 
	 * @param voucherInStockOrderId
	 *            入库单ID
	 * @return 凭证入库单明细记录数
	 */
	@Override
	public BigInteger getVoucherInStockOrderDetailByVoucherInStockOrderIdTotal(
			PCPSWebInput webInput, String voucherInStockOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String sql = "select nvl(count(*),0)  from VoucherInStockOrderDetail t where t.voucherInStockOrderId='"
				+ voucherInStockOrderId + "' order by t.SeqNo";

		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 新增凭证入库单明细
	 * 
	 * @param VoucherInStockOrderDetail
	 *            凭证入库单明细
	 * @throws PCPSApplicationException
	 */
	@Override
	public void insertVoucherInStockOrderDetail(
			VoucherInStockOrderDetail voucherInStockOrderDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherInStockOrderDetail);
	}

	/**
	 * 根据条件查询凭证入库单明细列表
	 * 
	 * @param voucherInStockOrderId
	 *            入库单ID
	 * @return 凭证入库单明细列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<VoucherInStockOrderDetail> getVoucherInStockOrderDetailByVoucherInStockOrderIdNotPage(
			PCPSWebInput webInput, BigInteger voucherInStockOrderId) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| voucherInStockOrderId == null)
			return null;
		String sql = "select * from VoucherInStockOrderDetail t where t.voucherInStockOrderId='"
				+ voucherInStockOrderId + "' order by t.SeqNo asc";
		Query query = _em.createNativeQuery(sql,
				VoucherInStockOrderDetail.class);
		return query.getResultList();
	}
	/**
	    * 根据序号段和凭证id查询号段是否存在
	    */
		@SuppressWarnings("unchecked")
		public BigInteger IsSeqNo(PCPSWebInput webInput, BigInteger voucherId,String startseqno,String endseqno){
			if (webInput == null || voucherId==null || PCPSUtil.isEmpty(startseqno) || PCPSUtil.isEmpty(endseqno))
				return BigInteger.ONE;
			String sql = "select *  from VoucherInStockOrderDetail t where  t.voucherId=?1" +
					"  and VOUCHERINSTOCKORDERID in(select VOUCHERINSTOCKORDERID from VOUCHERINSTOCKORDER where ISSUECOMPANYNO=?4)";			

			Query query = _em.createNativeQuery(sql,VoucherInStockOrderDetail.class);
			query.setParameter(1, voucherId);
			query.setParameter(4, webInput.getOperBranchNo().substring(0,6));
			List<VoucherInStockOrderDetail> dalist=query.getResultList();
			if(dalist==null || dalist.size()==0){//空
			    return BigInteger.ZERO;
			}else{
				VoucherInStockOrderDetail dil=null;
				for(int i=0;i<dalist.size();i++){
					dil=dalist.get(i);
					if(isVoucherNo(startseqno,endseqno,dil.getStartVoucherNo().trim(),dil.getEndVoucherNo().trim()).equals("error")) 
				        return BigInteger.TEN;
				}
				
			}
			return BigInteger.ZERO;
			   
		}
	/**
	    * 验证号段是否重复
	    */
		@SuppressWarnings("unchecked")
		private String isVoucherNo(String startvoucherno,String endvoucherno,String dbstartno,String dbendno){
			BigInteger templength = new BigInteger(endvoucherno).subtract(new BigInteger(startvoucherno));
			templength=templength.add(BigInteger.ONE);
			BigInteger verno= new BigInteger(startvoucherno);
			for(int i=0;i<templength.intValue();i++){
				verno=verno.add(new BigInteger(String.valueOf(i)));
				if(verno.intValue()>=Integer.parseInt(dbstartno) && verno.intValue()<=Integer.parseInt(dbendno)){						
					return "error";
			 	
				}
			}
			  return "";
		}
}
